//
//  JJGroupChatSelectAtController.swift
//  swiftProject
//
//  Created by yangjian on 2021/12/28.
//

import UIKit

let message_atter_list = "em_at_list"
//携带字符串定位,可告知在哪个地方@的你.
let message_atter_list_hasRange = "group_at_list"

let message_atter_atAll = "ALL"


class JJGroupChatSelectAtController: UIViewController {
    var groupid = ""
    var selectedMemberCompletion : (JJGroupChatMemberModel) -> Void
    var models = [JJGroupChatMemberModel].init()
    let tableView = UITableView.init(frame: CGRect.zero, style: .plain);
    init(groupid : String,selectedMemberCompletion:@escaping (JJGroupChatMemberModel) -> Void) {
        self.groupid = groupid
        self.selectedMemberCompletion = selectedMemberCompletion
        super.init(nibName: nil, bundle: nil)
    }
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    override func viewDidLoad() {
        super.viewDidLoad()
        config()
        self.loadData()
    }
    
    func config(){
        self.navigationItem.title = "请选择成员"
        self.view.backgroundColor = UIColor.white;
        self.tableView.register(JJGroupChatSelectAtCell.classForCoder(), forCellReuseIdentifier: NSStringFromClass(JJGroupChatSelectAtCell.classForCoder()))
        self.tableView.register(UITableViewCell.classForCoder(), forCellReuseIdentifier: NSStringFromClass(UITableViewCell.classForCoder()))
        self.tableView.delegate = self;
        self.tableView.dataSource = self;
        self.tableView.separatorStyle = .none;
        self.view.addSubview(self.tableView)
        self.tableView.snp.makeConstraints { make in
            make.top.equalTo(0);
            make.left.equalTo(0);
            make.bottom.equalTo(0);
            make.right.equalTo(0);
        }
    }
    
    func loadData(){
        EMClient.shared().groupManager!.getGroupSpecificationFromServer(withId: self.groupid, fetchMembers: true) { group, error in
            guard let group = group else {return }
//            let list = group.memberList
            
            var members : [String] = [String].init()
            members.append(group.owner)
            members += (group.adminList!)
            members += (group.memberList!)

            members = members.filter { username in
                username != EMClient.shared().currentUsername
            }
            EMClient.shared().userInfoManager!.fetchUserInfo(byId: members) { result, error in
                guard let result = result else {return }
                for username in members {
                    let model = JJGroupChatMemberModel.init()
                    model.username = username
                    let userInfo = result[username] as! EMUserInfo
                    model.nickName = userInfo.nickname ?? username
                    model.headImageURLString = userInfo.avatarUrl ?? ""
                    self.models.append(model)
                    queue_main.async {
                        self.tableView.reloadData()
                    }
                }
            }
        }
        self.tableView.reloadData();
    }
    
}

extension JJGroupChatSelectAtController : UITableViewDelegate,UITableViewDataSource{
    func numberOfSections(in tableView: UITableView) -> Int {
        return 2;
    }
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        if section == 0 {
            return 1;
        }else if section == 1 {
            return self.models.count;
        }
        return 0
    }
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        if indexPath.section == 0 {
            let cell = tableView.dequeueReusableCell(withIdentifier: NSStringFromClass(UITableViewCell.classForCoder()), for: indexPath)
            cell.textLabel!.text = "所有人"
            cell.imageView?.image = UIImage.init(named: "login_logo")
            return cell;
        }else if indexPath.section == 1 {
            let cell = tableView.dequeueReusableCell(withIdentifier: NSStringFromClass(JJGroupChatSelectAtCell.classForCoder()), for: indexPath) as! JJGroupChatSelectAtCell
            cell.bindModel(model: self.models[indexPath.row])
            return cell;
        }
        
        return UITableViewCell.init();
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.deselectRow(at: indexPath, animated: true)
        if indexPath.section == 0 {
            let model = JJGroupChatMemberModel.init()
            model.atAll = true
            model.nickName = "所有人"
            self.selectedMemberCompletion(model)
        }else if indexPath.section == 1 {
            self.selectedMemberCompletion(self.models[indexPath.row])
        }
        self.dismiss(animated: true) {
        }
    }
}
